﻿using System.Diagnostics;


namespace JobTools.Helper
{
    public class ThreadHelper
    {
        public static void RunInAdditionalThread(DelegateVoidMethod execMethod, DelegateVoidMethodWithParam onException = null, Control control = null)
        {
            bool flag = control != null;
            if (flag)
            {
                new Thread(delegate ()
                {
                    try
                    {
                        control.BeginInvoke(execMethod);
                    }
                    catch (Exception ex)
                    {
                        bool flag2 = onException != null;
                        if (flag2)
                        {
                            onException(ex);
                        }
                        else
                        {
                            Console.WriteLine(ex);
                        }
                    }
                })
                {
                    IsBackground = true
                }.Start();
            }
            else
            {
                ThreadPool.QueueUserWorkItem(delegate (object p)
                {
                    try
                    {
                        execMethod();
                    }
                    catch (Exception ex)
                    {
                        bool flag2 = onException != null;
                        if (flag2)
                        {
                            onException(ex);
                        }
                        Console.WriteLine(ex);
                    }
                }, null);
            }
        }

        public static void RunInAdditionalThread(DelegateVoidMethodWithParam execMethod, object param, DelegateVoidMethodWithParam onException = null, Control control = null)
        {
            bool flag = control != null;
            if (flag)
            {
                new Thread(delegate ()
                {
                    try
                    {
                        control.BeginInvoke(execMethod, new object[]
                        {
                            param
                        });
                    }
                    catch (Exception ex)
                    {
                        bool flag2 = onException != null;
                        if (flag2)
                        {
                            onException(ex);
                        }
                        else
                        {
                            Console.WriteLine(ex);
                        }
                    }
                })
                {
                    IsBackground = true
                }.Start();
            }
            else
            {
                ThreadPool.QueueUserWorkItem(delegate (object p)
                {
                    try
                    {
                        execMethod(p);
                    }
                    catch (Exception ex)
                    {
                        bool flag2 = onException != null;
                        if (flag2)
                        {
                            onException(ex);
                        }
                        Console.WriteLine(ex);
                    }
                }, param);
            }
        }


        public static void RunApp(string appName)
        {
            new Process
            {
                StartInfo =
                {
                    FileName = appName
                }
            }.Start();
        }

        public static void Delay(int millisecondsTimeout)
        {
            Thread.Sleep(millisecondsTimeout);
        }

        public static bool RunCmd(string cmdExe, string cmdStr)
        {
            bool result = false;
            try
            {
                using (Process process = new Process())
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo(cmdExe, cmdStr);
                    process.StartInfo = startInfo;
                    process.Start();
                    process.WaitForExit();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool RunCmd2(string cmdExe, string cmdStr)
        {
            bool result = false;
            try
            {
                using (Process process = new Process())
                {
                    process.StartInfo.FileName = "cmd.exe";
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();
                    string value = string.Format("\"{0}\" {1} {2}", cmdExe, cmdStr, "&exit");
                    process.StandardInput.WriteLine(value);
                    process.StandardInput.AutoFlush = true;
                    process.WaitForExit();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
